package pers.zwj;


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

@SuppressWarnings("all")
public class MyArtboard extends JPanel implements Runnable, MouseListener {

    public Vector<BlackChess> blackChesses = new Vector<>();
    public Vector<WhiteChess> whiteChesses = new Vector<>();
    public BlackChess blackChess = null;
    public WhiteChess whiteChess = null;
    /**
     * 偶数·代表黑方
     * 奇数.代表白方
     */
    public int colorChess;//按键棋子判断标志
    public String chess;//棋子方
    int x;
    int y;//储存鼠标点击位置
    public boolean canPlay;//是否游戏
    public boolean isOver;//是否绘制游戏结束
    public boolean isDrawPlay;//是否绘制再来新一局
    public MyArtboard() {
        colorChess = 2;
        chess = "黑方";
        canPlay = true;
        isOver = false;
        isDrawPlay = false;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.PINK);//设置棋盘颜色：粉色
        g.fillRect(0, 0, 460, 460);//画棋盘
        g.setColor(Color.cyan);
        g.fillRect(460, 0, 160, 172);
        g.fillRect(460, 212, 160, 460 - 212);
        g.fillRect(578, 160, 42, 60);
        g.fillRect(460, 160, 8, 60);
        drawBoard(g);//画棋盘线条
        drawBlackChess(g, blackChesses);//画黑棋
        drawWhiteChess(g, whiteChesses);//画白棋
        drawGameRules(g);//画游戏信息
    }

    /**
     * 画棋盘线条
     */
    public void drawBoard(Graphics g) {
        g.setColor(Color.black);
        for (int i = 20; i <= 440; i += 30) {
            g.drawLine(20, i, 440, i);
            g.drawLine(i, 20, i, 440);
        }

    }

    /**
     * 绘画游戏信息
     */
    public void drawGameRules(Graphics g) {

        g.setColor(Color.black);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);
        g.drawString("游戏规则:", 470, 30);
        g.setColor(Color.red);
        g.drawString("黑方先行！", 470, 60);
        g.setColor(Color.black);
        g.drawString("当前下棋方：", 470, 90);
        switch (chess) {
            case "黑方":
                g.setColor(Color.black);
                g.drawString(chess, 470, 120);
                break;
            case "白方":
                g.setColor(Color.white);
                g.drawString(chess, 470, 120);
                break;
        }

        //绘制他方最后一次下棋位置提醒
        switch (chess) {
            case "黑方":
                if (whiteChesses.size() == 0) break;
                int i = whiteChesses.size() - 1;
                WhiteChess whiteChess = whiteChesses.get(i);
                g.setColor(Color.black);
                int x = whiteChess.getX() - 12;
                int y = whiteChess.getY() - 12;
                int e = 5;
                g.drawLine(x, y, x + e, y);
                g.drawLine(x, y, x, y + e);
                g.drawLine(x, y + 24, x + e, y + 24);
                g.drawLine(x, y + 24, x, y + 24 - e);
                g.drawLine(x + 24, y, x + 24 - e, y);
                g.drawLine(x + 24, y, x + 24, y + e);
                g.drawLine(x + 24, y + 24, x + 24 - e, y + 24);
                g.drawLine(x + 24, y + 24, x + 24, y + 24 - e);
                break;
            case "白方":
                if (blackChesses.size() == 0) break;
                int j = blackChesses.size() - 1;
                BlackChess blackChess = blackChesses.get(j);
                g.setColor(Color.black);
                int x1 = blackChess.getX() - 12;
                int y1 = blackChess.getY() - 12;
                int e1 = 5;
                g.drawLine(x1, y1, x1 + e1, y1);
                g.drawLine(x1, y1, x1, y1 + e1);
                g.drawLine(x1, y1 + 24, x1 + e1, y1 + 24);
                g.drawLine(x1, y1 + 24, x1, y1 + 24 - e1);
                g.drawLine(x1 + 24, y1, x1 + 24 - e1, y1);
                g.drawLine(x1 + 24, y1, x1 + 24, y1 + e1);
                g.drawLine(x1 + 24, y1 + 24, x1 + 24 - e1, y1 + 24);
                g.drawLine(x1 + 24, y1 + 24, x1 + 24, y1 + 24 - e1);
                break;
        }
        g.setColor(Color.black);
        g.drawString("欢迎来到!", 470, 250);
        g.setColor(Color.red);
        Font font_ = new Font("宋体", Font.BOLD, 30);
        g.setFont(font_);
        g.drawString("欢乐五子棋", 460, 310);
        g.setColor(Color.blue);
        g.setFont(font);
        g.drawString("尊敬的玩家", 470, 370);
        g.drawString("请开始你的", 470, 400);
        g.drawString("五子棋之路!", 470, 430);
        switch (chess) {
            case "白方":
                g.setColor(Color.white);
                g.fillOval(530, 120 - 20, 24, 24);
                g.setColor(Color.black);
                g.drawOval(530, 120 - 20, 24, 24);
                break;
            case "黑方":
                g.setColor(Color.black);
                g.fillOval(530, 120 - 20, 24, 24);
                break;
        }
        if (isOver) {

            Font font1 = new Font("宋体", Font.BOLD, 50);
            g.setFont(font1);
            g.setColor(Color.red);
            g.drawString(chess + " 获胜！", 100, 150);
            g.drawString("游戏结束！", 120, 200);

        }
        g.setColor(Color.GREEN);
        g.fill3DRect(470, 200 - 25, 105, 35, false);
        Font font2 = new Font("宋体", Font.BOLD, 25);
        g.setFont(font2);
        g.setColor(Color.black);
        g.drawString("再来一局", 470, 200);
        if (isDrawPlay) {
            Font font1 = new Font("宋体", Font.BOLD, 50);
            g.setFont(font1);
            g.setColor(Color.red);
            g.drawString("开始新一局游戏!", 50, 200);
            g.drawString("在棋盘开始下棋!", 50, 270);
//            isDrawPlay = false;
        }


    }

    /**
     * 绘画黑色棋子
     */
    public void drawBlackChess(Graphics g, Vector<BlackChess> chesses) {

        g.setColor(Color.black);
        for (int i = 0; i < chesses.size(); i++) {
            BlackChess chess = chesses.get(i);
            g.fillOval(chess.getX() - 12, chess.getY() - 12, 24, 24);
        }


    }

    public void drawWhiteChess(Graphics g, Vector<WhiteChess> chesses) {

        for (int i = 0; i < chesses.size(); i++) {
            WhiteChess chess = chesses.get(i);
            g.setColor(Color.white);
            g.fillOval(chess.getX() - 12, chess.getY() - 12, 24, 24);
            g.setColor(Color.black);
            g.drawOval(chess.getX() - 12, chess.getY() - 12, 24, 24);
        }


    }

    @Override
    public void run() {
        while (true) {
            this.repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        //为什么这里用if不行？
        int tempX;
        int tempY;
        tempX = e.getX() - 7;
        tempY = e.getY() - 30;
        /**
         *判断重新开始
         */
        //第一次就按重新开始不会反应
        if (x != 0 && y != 0) {
            if (tempX > 470 && tempX < 575 && tempY > 175 && tempY < 210) {
                blackChesses.clear();
                whiteChesses.clear();
                canPlay = true;
                isOver = false;
                colorChess = 2;
                isDrawPlay = true;
                chess = "黑方";
                x = 0;
                y = 0;
                for (int i = 0; i < 17; i++) {
                    for (int j = 0; j < 17; j++) {
                        GoBangGame.chess[i][j] = 0;
                    }
                }
            }
        }

        if (tempX < 440 && tempX > 20 && tempY < 440 && tempY > 20) {
            for (int i = 20; i <= 440; i += 30) {
                for (int j = 20; j <= 440; j += 30) {
                    if (tempX < i + 15 && tempX > i - 15 && tempY < j + 15 && tempY > j - 15) {
                        x = i;
                        y = j;
                        isDrawPlay = false;//再次下棋，消失再来一局
                    }
                }
            }
        }

        if (canPlay && x != 0 && y != 0) {
            switch (colorChess % 2) {
                case 0: {
                    int i = (y - 20) / 30 + 1;
                    int j = (x - 20) / 30 + 1;
                    if (GoBangGame.chess[i][j] == 0) {
                        blackChess = new BlackChess(x, y);
                        blackChesses.add(blackChess);
                        GoBangGame.chess[i][j] = 1;
                        canPlay = isChess(i, j);

                        colorChess++;
                        chess = "白方";
                        if (!canPlay) chess = "黑方";
                        break;
                    }

                }
                case 1: {
                    int i = (y - 20) / 30 + 1;
                    int j = (x - 20) / 30 + 1;
                    if (GoBangGame.chess[i][j] == 0) {
                        whiteChess = new WhiteChess(x, y);
                        whiteChesses.add(whiteChess);
                        GoBangGame.chess[i][j] = 2;
                        canPlay = isChess(i, j);
                        colorChess++;
                        this.chess = "黑方";
                        if (!canPlay) chess = "白方";
                        break;
                    }

                }

            }
        }

    }

    public boolean isChess(int i, int j) {
        boolean is = true;
        if (!isHengChess(i, j) ||
                !isShuChess(i, j) ||
                !isXieChess1(i, j) ||
                !isXieChess2(i, j))
            is = false;
        return is;
    }

    public boolean isHengChess(int i, int j) {
        int count = 1;
        boolean is = true;
        int k = 1;
        //判断横向
        //判断右横向
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i + k][j]) {
            k++;
            count++;

        }
        //判断左横向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i - k][j]) {
            k++;
            count++;

        }
        if (count >= 5) {
            is = false;
            isOver = true;
        }
        return is;
    }

    public boolean isShuChess(int i, int j) {
        int count = 1;
        boolean is = true;
        int k = 1;
        //判断竖向
        //判断上竖向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i][j - k]) {
            k++;
            count++;

        }
        //判断下竖向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i][j + k]) {
            k++;
            count++;

        }
        if (count >= 5) {
            is = false;
            isOver = true;
        }
        return is;
    }

    public boolean isXieChess1(int i, int j) {
        int count = 1;
        boolean is = true;
        int k = 1;
        //判断斜向
        //判断右上斜向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i + k][j - k]) {
            k++;
            count++;

        }
        //判断左下斜向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i - k][j + k]) {
            k++;
            count++;

        }
        if (count >= 5) {
            is = false;
            isOver = true;
        }
        return is;
    }

    public boolean isXieChess2(int i, int j) {
        int count = 1;//判断是否胜利标志
        boolean is = true;//
        int k = 1;
        //判断左上斜向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i - k][j - k]) {
            k++;
            count++;

        }
        //判断右下斜向
        k = 1;
        while (GoBangGame.chess[i][j] == GoBangGame.chess[i + k][j + k]) {
            k++;
            count++;

        }
        if (count >= 5) {
            is = false;
            isOver = true;
        }
        return is;

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
